<template>
  <Modal
    v-model="form.visible"
    :width="1000"
    :mask-closable="false"
    :loading="loading"
    :title="'添加回复'"
    ok-text="提交"
    @on-ok="ok">
    <Row class="mt30">
      <Col span="16" offset="4">
        <Form ref="form" :model="form.data" :rules="ruleValidate" :label-width="120">
          <FormItem prop="reContent" label="回复内容">
            <Input type="textarea" v-model="form.data.reContent" placeholder="请输入回复内容" row="5"></Input>
          </FormItem>
          <FormItem prop="isShow" label="是否外网显示">
            <RadioGroup v-model="form.data.isShow">
              <Radio :label="'0'">是</Radio>
              <Radio :label="'1'">否</Radio>
            </RadioGroup>
          </FormItem>
        </Form>
      </Col>
    </Row>
  </Modal>
</template>

<script>
  import { mapGetters } from 'vuex'
  import activityApi from '@/api/activity'

  export default {
    name: 'update-modal',
    props: [
      'form'
    ],
    data () {
      return {
        loading: true,
        ruleValidate: {
          reContent: [
            { required: true, message: '请输入回复内容', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {
      ...mapGetters({
        userInfo: 'userInfo'
      })
    },
    watch: {
      'form.visible': 'onClose'
    },
    methods: {
      ok () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.submitForm()
          } else {
            this.$Message.error('表单验证失败，请检查输入项目!')
            this.form.visible = true
            this.loading = false
            setTimeout(() => {
              this.loading = true
            }, 100)
          }
        })
      },
      onClose (isVisible) {
        if (!isVisible) {
          this.$nextTick(() => {
            this.$refs.form.resetFields()
          })
        }
      },
      async submitForm () {
        this.form.data.rePkuid = this.userInfo.pkUid
        const { body } = await activityApi.message.reply(this.form.data)
        if (this.$apiHelper.requestSuccessed(body.responseCode)) {
          this.$Message.info('回复成功')
          this.form.visible = false
          this.$emit('submit')
        } else {
          setTimeout(() => {
            this.$Modal.error({
              title: '错误',
              content: body.desc,
              onOk: () => {}
            })
          }, 500)
        }
      }
    }
  }
</script>
